﻿using Common.Helpers;
using Common.Prism.Apps;
using Common.Prism.Events;
using NxChatQQ.Models;
using NxChatQQ.Models.Events;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace NxChatQQ.ViewModels.Layouts
{
    public class ChatMessageLayoutViewModel : BaseViewModel
    {
        private string _InputText = "";
        public string InputText
        {
            get { return _InputText; }
            set { SetProperty(ref _InputText, value); }
        }

        private string _NameTitle = "";
        public string NameTitle
        {
            get { return _NameTitle; }
            set { SetProperty(ref _NameTitle, value); }
        }

        private bool _IsFlyoutOpen = false;
        public bool IsFlyoutOpen
        {
            get { return _IsFlyoutOpen; }
            set { SetProperty(ref _IsFlyoutOpen, value); }
        }

        private bool _IsEmojiOpend = false;
        public bool IsEmojiOpend
        {
            get { return _IsEmojiOpend; }
            set { SetProperty(ref _IsEmojiOpend, value); }
        }

        public ICommand TxtSendCommand { get; set; }
        public ICommand BtnEmojiCommand { get; set; }
        public ICommand BtnScreenshotCommand { get; set; }
        public ICommand BtnFileCommand { get; set; }
        public ICommand BtnImageCommand { get; set; }
        public ICommand BtnRemindCommand { get; set; }
        public ICommand BtnMoneyCommand { get; set; }
        public ICommand BtnRecordAudioCommand { get; set; }


        public ICommand BtnMeetAudioCommand { get; set; }
        public ICommand BtnMeetVideoCommand { get; set; }
        public ICommand BtnScreenShareCommand { get; set; }
        public ICommand BtnScreenControlCommand { get; set; }
        public ICommand BtnCreateRoomCommand { get; set; }
        public ICommand BtnRightCommand { get; set; }



        public Action ScrollToBottomAction { get; set; }

        public ObservableCollection<MessageObject> MessageItems { get; set; }

        public ChatMessageLayoutViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            // 订阅事件
            eventAggregator.GetEvent<UpdateChatEvent>().Subscribe(UpdateChatMethod);

            TxtSendCommand = new DelegateCommand<string>(TxtSendClick);

            BtnEmojiCommand = new DelegateCommand(BtnEmojiClick);
            BtnScreenshotCommand = new DelegateCommand(BtnScreenshotClick);
            BtnFileCommand = new DelegateCommand(BtnFileClick);
            BtnImageCommand = new DelegateCommand(BtnImageClick);
            BtnRemindCommand = new DelegateCommand(BtnRemindClick);
            BtnMoneyCommand = new DelegateCommand(BtnMoneyClick);
            BtnRecordAudioCommand = new DelegateCommand(BtnRecordAudioClick);


            BtnMeetAudioCommand = new DelegateCommand(BtnMeetAudioClick);
            BtnMeetVideoCommand = new DelegateCommand(BtnMeetVideoClick);
            BtnScreenShareCommand = new DelegateCommand(BtnScreenShareClick);
            BtnScreenControlCommand = new DelegateCommand(BtnScreenControlClick);
            BtnCreateRoomCommand = new DelegateCommand(BtnCreateRoomClick);
            BtnRightCommand = new DelegateCommand(BtnRightClick);


            DateTime today = DateTime.Now; // 获取当前日期和时间  


            MessageItems = new ObservableCollection<MessageObject>();
            var text = new MessageObject();
            text.Content = "1111";
            text.IsGroup = false;
            text.FromUserName = "22222";
            text.Type = Models.Enums.kWCMessageType.Text;
            text.IsMysend = true;
            text.CanRevoke = true;
            text.TimeVisible = true;
            text.TimeSend = today.AddDays(-1);

            MessageItems.Add(text);

            text = new MessageObject();
            text.Content = "222";
            text.IsGroup = false;
            text.FromUserName = "3333";
            text.Type = Models.Enums.kWCMessageType.Text;
            text.IsMysend = false;
            text.TimeSend = today.AddDays(-3);
            text.TimeVisible = false;

            MessageItems.Add(text);
        }

        private void UpdateChatMethod(Friend friend)
        {
            NameTitle = friend.Name;
        }


        public void TxtSendClick(string txt)
        {
            var time = GetLastMessageTime();
            var now = DateTime.Now;
            var diff = (now - time).TotalSeconds;


            var text = new MessageObject();
            text.Content = txt;
            text.IsGroup = false;
            text.FromUserName = "22222";
            text.Type = Models.Enums.kWCMessageType.Text;
            text.IsMysend = true;
            text.CanRevoke = true;
            text.TimeSend = DateTime.Now;
            text.TimeVisible = diff > 60 * 15;
            MessageItems.Add(text);

            InputText = "";

            ScrollToBottomAction?.Invoke();
        }

        private DateTime GetLastMessageTime()
        {
            if (MessageItems != null && MessageItems.Count > 0)
            {
                return MessageItems[MessageItems.Count - 1].TimeSend;
            }
            return DateTime.MinValue;
        }



        #region 顶部控制栏

        private void BtnMeetAudioClick()
        {
            ShowTip("语音通话");
        }

        private void BtnMeetVideoClick()
        {
            ShowTip("视频通话");
        }

        private void BtnScreenShareClick()
        {
            ShowTip("屏幕分享");
        }

        private void BtnScreenControlClick()
        {
            ShowTip("屏幕控制");
        }

        private void BtnCreateRoomClick()
        {
            ShowError("群主不允许群成员邀请好友加群");
        }

        public void BtnRightClick()
        {
            ClickUtil.Throttle(() =>
            {
                IsFlyoutOpen = !IsFlyoutOpen;
            }, 500);
        }

        #endregion



        #region 功能栏按钮


        public void BtnEmojiClick()
        {
            // 节流处理，防止多次点击
            ClickUtil.Throttle(() =>
            {
                IsEmojiOpend = !IsEmojiOpend;
            }, 500);
        }


        public void BtnScreenshotClick()
        {
            ShowTip("点击了截图按钮");

        }

        public void BtnFileClick()
        {
            ShowWarn("点击了文件按钮");
        }

        public void BtnImageClick()
        {
            ShowError("点击了图片按钮");
        }

        private void BtnRemindClick()
        {

        }

        private void BtnMoneyClick()
        {
            var dialog = new DialogData();
            dialog.ViewName = "HongBaoView";
            dialog.AllowClose = true;
            ShowDialog(dialog);
        }

        private void BtnRecordAudioClick()
        {
            ShowTip("未检测到麦克风");
        }

        #endregion


    }
}
